package com.netdisk.controller;
/**
 * @author jack
 * 管理文件的重命名 ,共享
 * 删除等操作
 * 2013-5-19 22:24:51
 */

import static com.netdisk.util.RemoteAddress.getRemoteAddress;
import static com.netdisk.util.UUIDCreator.getUuId;

import java.util.Calendar;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.netdisk.entity.FileSystem;
import com.netdisk.entity.Log;
import com.netdisk.entity.User;
import com.netdisk.service.impl.FileSystemService;
import com.opensymphony.xwork2.ActionSupport;

@Component("fileManageAction")
@Scope("prototype")
public class FileManageAction extends ActionSupport{

	private FileSystemService fileSystemService;
	
	private String fileId ;
	
	private String currentDir;
	
	private String parentDir;
	
	private String newDir;
	
	private String result;
	
	private String fileName;
	
	@JSON(serialize=false)
	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	@Resource(name="fileSystemService")
	public void setFileSystemService(FileSystemService fileSystemService) {
		this.fileSystemService = fileSystemService;
	}
	
	@JSON(deserialize=false)
	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	@JSON(serialize=false)
	public String getCurrentDir() {
		return currentDir;
	}
	
	@JSON(serialize=false)
	public String getNewDir() {
		return newDir;
	}
	
	public void setNewDir(String newDir) {
		this.newDir = newDir;
	}

	public void setCurrentDir(String currentDir) {
		this.currentDir = currentDir;
	}
	
	@JSON(serialize=false)
	public String getParentDir() {
		return parentDir;
	}

	public void setParentDir(String parentDir) {
		this.parentDir = parentDir;
	}
	
	public String getFileId() {
		return fileId;
	}
	
	@JSON(serialize=false)
	public void setFileId(String fileId) {
		this.fileId = fileId;
	}
	
	//新建文件夹
	public String createFolder(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		System.out.println(user);
		String ip = getRemoteAddress(request);
		FileSystem fs = new FileSystem(getUuId(), user.getCompanyId(), user.getCompanyName(), this.currentDir, getUuId() , (byte)1,
				this.newDir, 0, Calendar.getInstance().getTimeInMillis(),
				Calendar.getInstance().getTimeInMillis(), 
				user.getId(), user.getLoginName(), (byte)1, 740 );
		Log log = null;
		if ( null != user.getCompanyId()){//企业级用户
			log = new Log(getUuId(), user.getCompanyId(), user.getCompanyName(),
					 user.getId(), user.getLoginName(), (byte)32,
					Calendar.getInstance().getTimeInMillis(), ip);
		}else{
			log = new Log(getUuId(), (byte)32,
					 user.getId(), user.getLoginName(), 
					Calendar.getInstance().getTimeInMillis(), ip);
		}
		fileSystemService.save(fs, log);
		return SUCCESS;
	}
	
	//重命名文件或者重命名文件
	public String renameFile(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession	session =  request.getSession();
		String ip = getRemoteAddress(request);
		FileSystem fs = fileSystemService.findById(this.fileId);
		fs.setFileName(this.fileName);
		Log fileLog = new Log(getUuId(), fs.getCompanyId(), fs.getCompanyName(),
				 fs.getOwnerId(), fs.getOwnerName(), (byte)34,
				Calendar.getInstance().getTimeInMillis(), ip);
		fileSystemService.save(fs, fileLog);
		result = SUCCESS;
		return SUCCESS;
	}
	
	public String deleteFile(byte opr){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession	session =  request.getSession();
		String ip = getRemoteAddress(request);
		FileSystem fs = fileSystemService.findById(this.fileId);
		fs.setStatus((byte)opr);
		Log fileLog = new Log(getUuId(), fs.getCompanyId(), fs.getCompanyName(),
				 fs.getOwnerId(), fs.getOwnerName(), (byte)33,
				Calendar.getInstance().getTimeInMillis(), ip);
		fileSystemService.save(fs, fileLog);
		result = SUCCESS;
		return SUCCESS;
	}
	
	public String recoverFile(){
//		HttpServletRequest request = ServletActionContext.getRequest();
//		HttpSession	session =  request.getSession();
//		String ip = getRemoteAddress(request);
//		FileSystem fs = fileSystemService.findById(this.fileId);
//		fs.setStatus((byte)1);
//		Log fileLog = new Log(getUuId(), fs.getCompanyId(), fs.getCompanyName(),
//				 fs.getOwnerId(), fs.getOwnerName(), (byte)34,
//				Calendar.getInstance(), ip);
//		fileSystemService.save(fs, fileLog);
//		result = SUCCESS;
		return deleteFile((byte)1);
	}
	
	/**
	 * 不共享文件
	 * oper 1 共享 ,0不共享
	 * @return
	 */
	public String deShareFile(byte opr){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession	session =  request.getSession();
		String ip = getRemoteAddress(request);
		FileSystem fs = fileSystemService.findById(this.fileId);
		fs.setShared((byte)opr);
		Log fileLog = new Log(getUuId(), fs.getCompanyId(), fs.getCompanyName(),
				 fs.getOwnerId(), fs.getOwnerName(), (byte)34,
				Calendar.getInstance().getTimeInMillis(), ip);
		fileSystemService.save(fs, fileLog);
		result = SUCCESS;
		return SUCCESS;
	}
	
	public String shareFile(){
		return deShareFile((byte)1);
	}
	
}
